
package geomatic.android;

import java.util.*;

import org.geonames.WikipediaArticle;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class WikipediaArticleListView extends ListView
{
	protected AbstractListAdapter adapter;
	
	public WikipediaArticleListView(final Context context, List<WikipediaArticle> articles)
	{
		this(context);
		setWikipediaArticles(articles);
	}

	public WikipediaArticleListView(Context context)
	{
		super(context);
		
		this.setOnItemClickListener(new AdapterView.OnItemClickListener()
		{

			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				WikipediaArticleView cview = (WikipediaArticleView) view;
				cview.toggle();
			}
			
		});
	}

	public void setWikipediaArticles(List<WikipediaArticle> articles)
	{
		adapter = new AbstractListAdapter(articles)
		{

			@Override
			public View getView(int position, View convertView, ViewGroup parent)
			{
				WikipediaArticle a = (WikipediaArticle) this.getItem(position);
				
				return new WikipediaArticleView( WikipediaArticleListView.this.getContext(), a);
			}
			
		};
		
		this.setAdapter(adapter);
		
	}

	public void clear()
	{
		this.setAdapter(null);
	}


}
